<template>
  <div>
    <div><b>用来做数据库配置</b></div>
    <hr>
    <el-collapse v-model="activeName" accordion @change="getdbconfig" :newvalue='activeName'>
      <template v-for="(value,idx) in sqlEnv">
        <el-collapse-item :title='value.name' :name=idx>
          <el-table :data="dbconfigs[value.id]">
            <el-table-column width="150" property="name" label="name"></el-table-column>
            <el-table-column width="150" property="host" label="host"></el-table-column>
            <el-table-column width="150" property="port" label="port"></el-table-column>
            <el-table-column width="150" property="username" label="username"></el-table-column>
            <el-table-column width="150" property="password" label="password"></el-table-column>
            <el-table-column fixed="right" label="操作" width="150" >
              <template slot-scope="{row,$index}">
                <el-button type="text" size="small" @click.native="handleEdit($index, row)" >编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button type="text" @click="adddbconfig(value.id)">添加数据库</el-button>
        </el-collapse-item>
      </template>
    </el-collapse>
    <hr>
    <div>
      <el-button @click="addSqlENV">添加环境</el-button>
    </div>
    <div>{{dbconfigs}}</div>
    <div>{{DbconfigForm}}</div>
    <el-dialog title="数据库修改/新增" :visible.sync="configDialogVisible" width="30%" :before-close="handleClose" >



    <el-form :model="DbconfigForm" :rules="dbconfigformRules" ref="DbconfigForm" label-width="100px" class="demo-ruleForm" label-position="left">
      <el-form-item label="dbname" prop="name"> <el-input v-model="DbconfigForm.name"></el-input></el-form-item>
      <el-form-item label="ip" prop="host"> <el-input v-model="DbconfigForm.host"></el-input></el-form-item>
      <el-form-item label="port" prop="port"> <el-input v-model="DbconfigForm.port"></el-input></el-form-item>
      <el-form-item label="username" prop="username"> <el-input v-model="DbconfigForm.username"></el-input></el-form-item>
      <el-form-item label="password" prop="password"> <el-input v-model="DbconfigForm.password"></el-input></el-form-item>

    </el-form>
      <div slot="footer" class="dialog-footer">
    <el-button @click="configDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="saveDbconfigForm('DbconfigForm')">确 定</el-button>
      </div>



    </el-dialog>

  </div>
</template>

<script>
  import vue from 'vue'
  import {
    getENVConfigapi,
    addENVConfigapi,
    getDbConfigapi,
    addDbConfigapi,
    updateDbConfigapi
  } from '../../api/sqlconfig'


  export default {
    name: "dbconfig",
    data() {
      return {
        activeName: '1',
        sqlEnv: [],
        dbconfigs: {},
        showbtn:[],
        envid:2,
        DbconfigForm:{
          id:0,
          envid:0,
          name:'',
          host:"",
          username:'',
          password:'',
          port:'',
          status:0
        },
        defaultDbconfigForm:{
          id:0,
          envid:0,
          name:'',
          host:"",
          username:'',
          password:'',
          port:'',
          status:0
        },
        configDialogVisible:false,
        dbconfigformRules:{
          name:[{ required: true, message: '字段不能为空', trigger: 'blur' },],
          host:[{ required: true, message: '字段不能为空', trigger: 'blur' },],
          port:[{ required: true, message: '字段不能为空', trigger: 'blur' },],
          username:[{ required: true, message: '字段不能为空', trigger: 'blur' },],
          password:[{ required: true, message: '字段不能为空', trigger: 'blur' },],
        },
      };
    },
    methods: {
      getdbconfig(val) {
        //这里输出打开的栏目顺序
        console.log(val)
        if (val !== null & val !== "") {
          if (!this.dbconfigs[this.sqlEnv[val].id]) {
            getDbConfigapi(this.sqlEnv[val].id)
              .then(res => {
                //  debugger;
                vue.set(this.dbconfigs, this.sqlEnv[val].id, res.data)
              }).catch(err => {
              console.log(err)
            })
          }
        }
      },
      saveDbconfigForm(formName){
        const _that = this;

        //this.$refs[formName].validate()
        this.$refs[formName].validate ((valid) =>{
         // console.log(_that,"2564855");

          if (valid) {
            //console.log(formName,"123456789")
            // debugger
            if (_that.DbconfigForm.id===0)  {//新增
              addDbConfigapi(_that.DbconfigForm)
                .then(res=>{
                  getDbConfigapi(res.data.envid) .then(res => {
                    //  debugger;
                    vue.set(this.dbconfigs, _that.DbconfigForm.envid, res.data)
                  }).catch(err => {
                    console.log(err)
                  })

                })
            }else{//修改
              updateDbConfigapi(_that.DbconfigForm)
                .then(res=>{
                  getDbConfigapi(res.data.envid) .then(res => {
                    //  debugger;
                    vue.set(this.dbconfigs,  _that.DbconfigForm.envid, res.data)
                  }).catch(err => {
                    console.log(err)
                  })
                })
            }
            console.log(_that.dbconfigs,"999999")
            _that.configDialogVisible=false
            // alert('submit!');
          } else {
            console.log('error submit!!');
            alert('11submit!');
            return false;
          }
        });
      },
      addSqlENV() {
        //  vue.set(this.sqlEnv, val, [{host: 1111}])
        this.$prompt('请输入新环境名称', '', {confirmButtonText: '确定', cancelButtonText: '取消',})
          .then(messagedata => {
            this.$message({type: 'success', message: `添加成功`});
            addENVConfigapi({"name": messagedata.value})
              .then(rep => {
                  getENVConfigapi().then(res => {
                    this.sqlEnv = res.data;
                    console.log(res.data)
                  });
                }
              ).catch(() => {
              this.$message({type: 'info', message: '添加失败，请重试'});
            })
          }).catch(() => {
          this.$message({type: 'info', message: '取消输入'});
        });
      },
      getENVConfig() {
        //  this.sqlEnv = '';
        getENVConfigapi().then(res => {

          this.sqlEnv = res.data;
          console.log(JSON.stringify(res.data))
        }).catch(err => {
          console.log(err)
        })
      },

      adddbconfig(val) {
        this.DbconfigForm=JSON.parse(JSON.stringify(this.defaultDbconfigForm))
        this.DbconfigForm.envid=val
        this.configDialogVisible=true
       // this.$alert('弹出一个数据库',)
      },
      handleEdit(index,row){
        console.log(index)
        console.log(JSON.stringify(row))
        this.configDialogVisible=true;
        this.DbconfigForm=JSON.parse(JSON.stringify(row));
      },
      resetForm(fields){
        this.$nextTick(()=>{
          this.$refs[fields].resetFields()
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      }
    ,
      update() {


      }


    },
    created() {

    },
    mounted() {
      this.getENVConfig();

    }


  }
</script>

<style scoped>

</style>
